import sys
import os
import requests
import requests.utils
import time
import logging

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
# from common import util
import traceback
from fake_useragent import UserAgent
from report_system.spider import spider_config

requests.packages.urllib3.disable_warnings()


class RequestEx(object):
    user_agent_ = None
    ua_ = UserAgent(path='%s/fake_useragent.json' % os.path.dirname(__file__))

    def get(self,
            url,
            expect_codes=200,
            not_expect_codes=429,
            method='GET',
            max_retry=spider_config.request_ex_max_retry_,
            retry_interval_sec=spider_config.spider_retry_interval_sec_,
            timeout_sec=spider_config.spider_timeout_sec_,
            **kwargs):

        if 'allow_redirects' not in kwargs:
            kwargs['allow_redirects'] = False

        if isinstance(expect_codes, int):
            expect_codes = [expect_codes]
        if 'allow_redirects' in kwargs and kwargs['allow_redirects'] and expect_codes is not None:
            expect_codes.append(302)

        if isinstance(not_expect_codes, int):
            not_expect_codes = [not_expect_codes]

        retry = 0
        r = requests.Response()
        r.status_code = 0

        while 1:
            try:
                s = requests.session()

                if 'verify' not in kwargs:
                    s.verify = False

                r = s.request(method=method, url=url, timeout=timeout_sec, **kwargs)
                # logging.info('expect_codes=%s, r.status_code=%d, retry=%d', str(expect_codes), r.status_code, retry)
                # 期望状态码
                if expect_codes is None or r.status_code in expect_codes:
                    break
                # 不期望状态码
                if not_expect_codes is not None and r.status_code not in not_expect_codes:
                    break
                err_msg = '%s 请求失败, status_code=%d, text=%s' % (url, r.status_code, r.text)
            except Exception as e:
                err_msg = '%s 请求失败, %s' % (url, str(e))
            if retry >= max_retry:
                logging.error(err_msg)
                break
            retry += 1
            time.sleep(retry_interval_sec)
        return r


def main():
    try:
        # util.init_logging()
        req_ex = RequestEx()
        r = req_ex.get(url='https://cn.bing.cossm/')
        logging.info(r.text)
    except Exception as e:
        logging.error('异常, %s, %s' % (repr(e), traceback.format_exc()))


if __name__ == '__main__':
    main()
